package com.example.yzx.allriddle;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class AiActivity extends AppCompatActivity {
    private TextView tc;
    private EditText edit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ai);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        tc=findViewById(R.id.ct);
        edit=findViewById(R.id.edit);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void find(View view){
        String edits=edit.getText().toString();
        okhttp(edits);
    }
    public void okhttp(final String type){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client=new OkHttpClient();
                    String url="https://api.jisuapi.com/iqa/query?appkey=d21f4e56d0a37ab5&question="+type+"";
                    Request request=new Request.Builder()
                            .url(url)
                            .build();
                    Response response=client.newCall(request).execute();
                    String data=response.body().string();
                    json(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void json(String data){
        try {
            JSONObject json = new JSONObject(data);
            JSONObject result = json.getJSONObject("result");
            String content=result.getString("content");
            Message message = hand.obtainMessage();
            message.what = 1;
            message.obj = content;
            hand.sendMessage(message);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                System.out.println("点击查询");
                String title = "      "+(String) msg.obj;
                tc.setText(title);
            }
        }

    };
}
